 //
// var BASE_URL = 'http://192.168.0.175/index.php?s=';
// var BASE_URL_2 = 'http://192.168.0.175/';
var BASE_URL = 'http://yin2.tanfangzi.com/index.php?s=';
var BASE_URL_2 = 'http://yin2.tanfangzi.com';

var SITE_URL = '';

//微信支付 回调
var wxPayUrl_1 = 'http://taoke.maiyatang.info/App/Notify/wxMallPaySuccessNotifyOne';
var wxPayUrl_2 = 'http://taoke.maiyatang.info/App/Notify/wxMallPaySuccessNotify';


//获取用户id
function getUserId(){
	var id = api.getPrefs({
		sync: true,
		key: 'userId'
	});
    if (id) {
        return id;
    } else {
        return false;
    }
}
function log(n){
	console.log(JSON.stringify(n));
}
//获取用户信息
function getUserinfo() {
	var userInfo = api.getPrefs({
		sync: true,
		key: 'userInfo'
	});
    if (!userInfo) {
        return false;
    } else {
        return JSON.parse(userInfo);
    }
}
//获取用户类型   1=普通用户	2=vip 3=运营商 4=超级运营商
function getUserVip(){
	var info = getUserinfo();
	return parseInt(info.user_vip);
}
//存取数据
function setGlobalData(key,val){
	api.setGlobalData({
		key: key,
		value: val
	});
}
function getGlobalData(name){
	var info = api.getGlobalData({
		key: name
	});
	if (!info) {
        return false;
    } else {
        return info;
    }
}
function setPrefs(key,val){
	api.setPrefs({
		key: key,
		value: val
	});
}
function getPrefs(name){
	var info = api.getPrefs({
		sync: true,
		key: name
	});
    if (!info) {
        return false;
    } else {
        return JSON.parse(info);
    }
}
//重新获取用户信息
function reLogin() {
    api.ajax({
        url: BASE_URL + 'App/User/userInfo',
        method: 'post',
        data: {
            values: {
                userid: getUserId()
            }
        }
    }, function(ret, err) {
        if (ret) {
            if (ret.code == 1) {
				api.setPrefs({
					key: 'userInfo',
					value: ret.data
				});
                api.sendEvent({
                    name: 'reLoginEvent'
                });
            }
        }
    });
}
// 跳转普通页面
function openWin(name){
	api.openWin({
	    name: name,
	    url: name+'.html',
		pageParam: api.pageParam
	});
}

//默认toast
function toast(msg){
	api.toast({
		msg: msg,
		duration: 2000,
		location: 'middle'
	});
}
//加载框
function showProgress(flag){
	api.showProgress({
		title: '请稍候...',
		text: '',
		modal: flag
	});
}
function hideProgress(){
	api.hideProgress();
}

//ajax封装
var _Ajax = function(method,url,head,data,callback){
	api.ajax({
		url: BASE_URL + url,
		method: method,
		headers: head,
		returnAll: true,
		data: {
			values: data
		}
	},function(ret,err){
		callback(ret,err);
	});
}

function changeData(dom,txt){
	$$(dom).text(txt);
}



// 数量加减
// function minus(el){
// 	var numBox = $$(el).parents('.cartNumBox');
// 	var num = parseInt(numBox.find('.num').text());
// 	num--;
// 	num = num<1 ? 1 : num;
// 	numBox.find('.num').text(num);
// }
// function plus(el){
// 	var numBox = $$(el).parents('.cartNumBox');
// 	var num = parseInt(numBox.find('.num').text());
// 	num++;
// 	numBox.find('.num').text(num);
// }

//html转义
function HTMLDecode(text) {
	var temp = document.createElement("div");
	temp.innerHTML = text;
	var output = temp.innerText || temp.textContent;
	temp = null;
	return output;
}
//textaea 换行
function line2br(str){
	var text = '';
	if(str){
		text = str.split("\n").join("<br />");
	}
	return text;
}

// 打开弹窗页面
function showMask(){
	$$('.popOverlay').addClass('show');
	$$('.popBox').addClass('show');
}
// 关闭弹窗页面
function closeMask(){
	$$('.popOverlay').removeClass('show');
	$$('.popBox').removeClass('show');
	setTimeout(function(){
		api.closeFrame();
	},250);
}

//自定义 alert 页面
function openAlertMask(msg){
	if( !msg ){
		return;
	}
	api.openFrame({
		name: 'mask_alert',
		url: 'widget://html/mask_alert.html',
		rect: {
			x: 0,
			y: 0,
			w: 'auto',
			h: 'auto'
		},
		bounces: false,
		animation: {
			type: 'fade',
			duration:300
		},
		pageParam: {
			msg: msg
		}
	});
}
//自定义 confirm弹窗 页面
//var confirmMaskTxt = ['最多可选9张照片','取消','确认'];
//openConfirmMask(confirmMaskTxt,'del()');
function openConfirmMask(data,callback,cancel){
	if( !data ){
		return;
	}
	var msg = data[0];
	var left = data[1] ? data[1] : '取消';
	var right = data[2] ? data[2] : '确认';
	api.openFrame({
		name: 'mask_all',
		url: 'widget://html/mask_all.html',
		rect: {
			x: 0,
			y: 0,
			w: 'auto',
			h: 'auto'
		},
		bounces: false,
		animation: {
			type: 'fade',
			duration:300
		},
		pageParam: {
			wname: api.winName,
			fname: api.frameName,
			msg: msg,
			left: left,
			right: right,
			fun: callback,
			cancelFn: cancel
		}
	});
}
// 打开商品详情
function openProductDetail(n){
	api.openWin({
		name: 'product_detail_win',
		url: 'widget://html/best/product_detail_win.html',
		reload: true,
		pageParam: {
			shopId: n
		}
	});
}
//打开大礼包详情页
function openGoodsDetail(n){
	api.openWin({
		name: 'gift_detail_win',
		url: 'widget://html/giftBag/gift_detail_win.html',
		pageParam: {
			godId: n
		}
	});
}
//打开积分详情页
function openUbiDetail(n){
	api.openWin({
		name: 'u_detail_win',
		url: 'widget://html/giftBag/u_detail_win.html',
		pageParam: {
			godId: n
		}
	});
}
//数组去重
function unique(arr){
	// 遍历arr，把元素分别放入tmp数组(不存在才放)
	var tmp = new Array();
	for(var i in arr){
		//该元素在tmp内部不存在才允许追加
		if(tmp.indexOf(arr[i])==-1){
			tmp.push(arr[i]);
		}
	}
	return tmp;
}

//获取优惠券 价格
function getCouponNum(str){
	var num = '';
	if(str){
		var ind = str.indexOf("减");
		var num = parseInt(str.substring(ind+1));
	}
	return num;
}


// 将js对象转成url jquery实现
// var obj={
//     id:1,
//     name:"张三",
//     age:10
// };
// console.log(parseParam(obj))
// $("#btn-a").click(function(){
//     window.location.href="2.html?"+parseParam(obj);
// });
function parseParam(paramObj, key){
    var paramStr="";
    if(paramObj instanceof String||paramObj instanceof Number||paramObj instanceof Boolean){
      paramStr+="&"+key+"="+encodeURIComponent(paramObj);
    }else{
      $.each(paramObj,function(i){
        var k=key==null?i:key+(paramObj instanceof Array?"["+i+"]":"."+i);
        paramStr+='&'+parseParam(this, k);
      });
    }
    return paramStr.substr(1);
  };

//根据参数名称获取url参数
function getUrlParamValue(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return decodeURIComponent(r[2]);
    return null;
}

//获取url参数封装成对象  var obj=GetRequest();
function GetRequest() {

  var url = location.search; //获取url中"?"符后的字串
   var theRequest = new Object();
   if (url.indexOf("?") != -1) {
      var str = url.substr(1);
      strs = str.split("&");
      for(var i = 0; i < strs.length; i ++) {
         theRequest[strs[i].split("=")[0]]=decodeURIComponent((strs[i].split("=")[1]));
      }
   }
   return theRequest;
}

//打开登录界面
function openLogin(){
	api.openWin({
		name: 'login_banner',
		url: 'widget://html/login_banner.html',
		bounces: false,
		slidBackEnabled: false,
		animation: {
			type: 'fade',
			duration:300
		}
	});
}
//打开购物车
function openCart(){
	api.openWin({
		name: 'cart_win',
		url: 'widget://html/giftBag/cart_win.html'
	});
}
//购物车角标
function cartDot(){
	var _CART_DATA_ = [];
	if(getPrefs('_CART_DATA_')){
		_CART_DATA_ = getPrefs('_CART_DATA_');
	}
	if(_CART_DATA_.length > 0){
		$$('.cartBadge').text(_CART_DATA_.length);
		$$('.cartBadge').removeClass('aui-hide');
	}else{
		$$('.cartBadge').addClass('aui-hide');
	}
}
function cartUcionDot(){
	var _CART_DATA_U_ = [];
	if(getPrefs('_CART_DATA_U_')){
		_CART_DATA_U_ = getPrefs('_CART_DATA_U_');
	}
	if(_CART_DATA_U_.length > 0){
		$$('.cartBadge').text(_CART_DATA_U_.length);
		$$('.cartBadge').removeClass('aui-hide');
	}else{
		$$('.cartBadge').addClass('aui-hide');
	}
}
//打开积分购物车
function openUbiCart(){
	api.openWin({
		name: 'u_cart_win',
		url: 'widget://html/giftBag/u_cart_win.html'
	});
}
//回到首页
function goHome(n){
	api.execScript({
        name: 'root',
        script: 'openHome(' + n + ');'
    });
    setTimeout(function() {
        api.closeToWin({
            name: 'root'
        });
    }, 50);
}
//打开文章详情
function openPostDetail(n){
	api.openWin({
		name: 'post_detail_win',
		url: 'widget://html/post/post_detail_win.html',
		reload: true,
		pageParam: {
			id: n
		}
	});
}
//根据实际like数量 返回 1w+
function getLikeNumStr(n){
	var str = parseInt(n);
	if(str >= 10000){
		str = parseFloat(str/10000).toFixed(1);
		str = str+'万'
	}
	return str;
}


//下拉刷新 图片
var refreshHeader_img = {
	pull: [
		'widget://image/loading/1.png',
		'widget://image/loading/1.png',
		'widget://image/loading/1.png',
		'widget://image/loading/1.png',
		'widget://image/loading/1.png',
		'widget://image/loading/1.png',
		'widget://image/loading/1.png',
		'widget://image/loading/1.png',
		'widget://image/loading/1.png',
		'widget://image/loading/2.png',
		'widget://image/loading/3.png',
		'widget://image/loading/4.png',
		'widget://image/loading/5.png',
		'widget://image/loading/6.png',
		'widget://image/loading/7.png',
		'widget://image/loading/8.png',
		'widget://image/loading/9.png',
		'widget://image/loading/10.png',
		'widget://image/loading/11.png',
		'widget://image/loading/12.png',
		'widget://image/loading/13.png'
	],
	load: [
		'widget://image/loading/l1.png',
		'widget://image/loading/l2.png',
		'widget://image/loading/l3.png'
	]
};
